package ink.ykb.j2CacheDemo.interceptors;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import ink.ykb.j2CacheDemo.util.Constant;
import ink.ykb.j2CacheDemo.util.HttpRequestLocal;

public class LoginInterceptor implements HandlerInterceptor{

	private HttpRequestLocal httpRequestLocal;

	public LoginInterceptor(HttpRequestLocal httpRequestLocal) {
		super();
		this.httpRequestLocal = httpRequestLocal;
	}

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		
		httpRequestLocal.set(request);
		
		Integer userId = (Integer)httpRequestLocal.getSessionValue(Constant.SESSION_USER_ID);
		if(userId == null){
			response.sendRedirect(httpRequestLocal.getAppURL()+"/login");
			return false;
		}
		
		return true;
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		// TODO Auto-generated method stub
	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		// TODO Auto-generated method stub
		
	}

	
}
